package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.YlUserMapper;
import com.jiyun.model.user.YlUser;
import com.jiyun.result.Result;
import com.jiyun.service.IYlUserService;
import com.jiyun.utils.AppJwtUtil;
import com.jiyun.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-20
 */
@Service
@Slf4j
public class YlUserServiceImpl extends ServiceImpl<YlUserMapper, YlUser> implements IYlUserService {

    @Override
    public Result login(YlUser user) {

        if (user == null) {return Result.fail("参数为获取到");}
        if (StringUtils.isEmpty(user.getName())){return Result.fail("用户名不能为空");}
        if (StringUtils.isEmpty(user.getPassword())){return Result.fail("密码不能为空");}

        //查找用户信息
        YlUser one = this.getOne(new QueryWrapper<YlUser>().eq("name", user.getName()));
        if (one == null){return Result.fail("用户不存在");}
//        判断密码
        String pwd = MD5Utils.encodeWithSalt(user.getPassword(), one.getSalt());
        log.info("密码："+pwd);
        if (!pwd.equals(one.getPassword())){return Result.fail("密码错误");}
//        生成token
        String token = AppJwtUtil.getToken(one.getId());
//        返回：token user
        Map data = new HashMap();
        data.put("user",user.getName());
        data.put("token",token);
        return Result.ok(data);

    }
















}
